module DreamSpace.System.Collections {
    // =======================================================================================================================

    ///** A fast stack-type array, where items are pushed and popped from the end only.
    //* This object is faster than using an array that keeps changing it size when a lot of items are added and removed. As items
    //* are popped from the stack, the size doesn't shrink, so there's less overhead.
    //*/
    //export class FastStack<T> {
    //    items: T[];
    //    length: number = 0;
    //    private __initialSize: number;

    //    constructor(size: number);
    //    constructor(items: any[]);
    //    constructor(..._items: any[]) {
    //        if (arguments.length > 0) {
    //            if (_items.length == 1 && typeof _items[0] == "number") {
    //                this.__initialSize = _items[0];
    //            } else {
    //                this.items = _items; // (since an array was given, populate now)
    //                this.__initialSize = _items.length;
    //            }
    //        }

    //    }

    //    push(item: T): number {
    //        if (!this.items)
    //            if (this.__initialSize)
    //                this.items = new Array(this.__initialSize);
    //            else
    //                this.items = []; // (only create another internal object when it is actually needed; this test is done only ONCE)
    //        FastStack.prototype.push = function (item: T): number {
    //            if (this.length == this.items.length) {
    //                this.items.push(item);
    //                return ++this.length;
    //            }
    //            else {
    //                this.items[this.length++] = item;
    //                return this.length - 1;
    //            }
    //        };
    //        return this.push(item);
    //    }

    //    pop(): T {
    //        if (this.length == 0) return UNDEFINED;
    //        return this.items[--this.length];
    //    }
    //}

    // =======================================================================================================================

    export interface NotifyCollectionChangingEventHandler<TItem> { (sender: {}, oldItems: TItem[], oldIndex: number, newItems: TItem[], newIndex: number): any }
    export interface NotifyCollectionChangedEventHandler<TItem> { (sender: {}, oldItems: TItem[], oldIndex: number, newItems: TItem[], newIndex: number): any }

    export interface INotifyCollectionChanging<TOwner, TItems> {
        collectionChanging: Events.EventDispatcher.$Type<TOwner, NotifyCollectionChangingEventHandler<TItems>>;
    }

    export interface INotifyCollectionChanged<TOwner, TItems> {
        collectionChanged: Events.EventDispatcher.$Type<TOwner, NotifyCollectionChangedEventHandler<TItems>>;
    }

    /** Holds an array of items, and implements notification functionality for when the collection changes. */
    export class ObservableCollection<TOwner, T> extends Array.$Type<T> implements INotifyCollectionChanging<TOwner, T>, INotifyCollectionChanged<TOwner, T>  {
        [name: string]: any;

        collectionChanging: Events.EventDispatcher.$Type<TOwner, NotifyCollectionChangingEventHandler<T>>; // TODO: Implement
        collectionChanged: Events.EventDispatcher.$Type<TOwner, NotifyCollectionChangedEventHandler<T>>; // TODO: Implement

        constructor() {
            super();
        }

        /**
            * Removes the last element from an array and returns it.
            */
        pop(): T {
            return global.Array.prototype.pop.call(this);
        }

        /**
            * Appends new elements to an array, and returns the new length of the array.
            * @param items New elements of the Array.
            */
        push(...items: T[]): number {
            return global.Array.prototype.push.call(this/*,...items*/);
        }

        /**
            * Reverses the elements in an Array. 
            */
        reverse(): T[] {
            return global.Array.prototype.reverse.call(this);
        }

        /**
            * Removes the first element from an array and returns it.
            */
        shift(): T {
            return global.Array.prototype.shift.call(this);
        }

        /** 
            * Returns a section of an array.
            * @param start The beginning of the specified portion of the array.
            * @param end The end of the specified portion of the array.
            */
        slice(start?: number, end?: number): T[] {
            return global.Array.prototype.slice.call(this, start, end);
        }

        /**
            * Sorts an array.
            * @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
            */
        sort(compareFn?: (a: T, b: T) => number): T[] {
            return global.Array.prototype.sort.call(this, compareFn);
        }

        /**
            * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
            * @param start The zero-based location in the array from which to start removing elements.
            */
        splice(start: number): T[];
        /**
            * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
            * @param start The zero-based location in the array from which to start removing elements.
            * @param deleteCount The number of elements to remove.
            * @param items Elements to insert into the array in place of the deleted elements.
            */
        splice(start: number, deleteCount: number, ...items: T[]): T[];
        splice(start: number, deleteCount?: number, ...items: T[]): T[] {
            return global.Array.prototype.splice.call(this, start, deleteCount/*,...items*/);
        }

        /**
            * Inserts new elements at the start of an array.
            * @param items  Elements to insert at the start of the Array.
            */
        unshift(...items: T[]): number {
            return global.Array.prototype.unshift.call(this, items);
        }

        /**
            * Returns the index of the first occurrence of a value in an array.
            * @param searchElement The value to locate in the array.
            * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
            */
        indexOf(searchElement: T, fromIndex?: number): number {
            // ... use the object.equals() comparer if object is of type System.Object (otherwise use native function) ...
            return global.Array.prototype.indexOf.call(this, searchElement, fromIndex);
        }

        /**
            * Returns the index of the last occurrence of a specified value in an array.
            * @param searchElement The value to locate in the array.
            * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
            */
        lastIndexOf(searchElement: T, fromIndex?: number): number {
            // ... use the object.equals() comparer if object is of type System.Object (otherwise use native function) ...
            return global.Array.prototype.lastIndexOf.call(this, searchElement, fromIndex);
        }

        /**
            * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
            * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
            * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
            */
        reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
        /**
            * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
            * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
            * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
            */
        reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
        reduce(callbackfn: any, initialValue?: T): T {
            return global.Array.prototype.reduce.call(this, callbackfn, initialValue);
        }

        /** 
            * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
            * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. 
            * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
            */
        reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
        /** 
            * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
            * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. 
            * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
            */
        reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T;
        reduceRight(callbackfn: any, initialValue?: T): T {
            return global.Array.prototype.reduceRight.call(this, callbackfn, initialValue);
        }
    }

    export interface ICollection<TOwner, T> extends ObservableCollection<TOwner, T> { }

    // =======================================================================================================================

} // (end Collections)
